#!/usr/bin/perl

use strict;
use warnings;

my %hash;
my %hash2;
while(my $line=<>){
    chomp($line);
    $line = lc($line);
    $line =~ s/^\s*([\d\D]*?)\s*$/$1/;
    $line =~ /^([\d\D]*?)\s+([\d\D]*)/;
    if($hash{$1}){
        $hash{$1}++;
    }else{
        $hash{$1} = 1;
    }
    my $bran  = $1;
    my $model = $2 || "不明";
    $model =~ /^([a-z]*\d*)/;
    $model = $1;
    if($hash2{"$bran $model"}){
        $hash2{"$bran $model"}++;
    }else{
        $hash2{"$bran $model"} = 1;
    }
}
my $total = 0;
my @brands;
foreach my $key(keys %hash){
    my %brand;
    $brand{brand} = $key;
    $brand{value} = $hash{$key};
    push @brands, \%brand;
}

@brands = sort {$b->{value} <=> $a->{value}} @brands;

foreach(@brands){
    print "$_->{brand} $_->{value}\n";
}

print "==================================================================\n";

my @models;
foreach my $key(keys %hash2){
    $key =~ /^([\d\D]*?)\s([\d\D]*?)$/;
    my %model;
    $model{brand} = $1;
    $model{val}   = $hash{$model{brand}};
    $model{model} = $2 || "不明";
    $model{value} = $hash2{$key};
    push @models, \%model;
}

@models = sort model_sort @models;

sub model_sort {
    return $b->{val} <=> $a->{val} || $b->{brand} cmp $a->{brand} || $b->{value} <=> $a->{value};
}

for(0..@models-1){
    print "$models[$_]->{brand} $models[$_]->{model} $models[$_]->{value}\n";
    $total += $models[$_]->{value};
}

print $total;


